package com.swsc.solarverse.service.openapi.impl;

import com.swsc.solarverse.common.util.ObjectMapper;
import com.swsc.solarverse.dao.entity.forward.*;
import com.swsc.solarverse.model.openapi.OpenApiForwardDeviceCombinerboxDataReqVO;
import com.swsc.solarverse.model.openapi.OpenApiForwardDeviceEnvironmentDataReqVO;
import com.swsc.solarverse.model.openapi.OpenApiForwardDeviceInverterDataReqVO;
import com.swsc.solarverse.model.openapi.OpenApiForwardDeviceStringDataReqVO;
import com.swsc.solarverse.service.forward.*;
import com.swsc.solarverse.service.openapi.OpenApiForwardDeviceDataService;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author chencl
 * @Date 2025/3/3 14:14
 * @Version 1.0
 * @Description
 */
@lombok.extern.slf4j.Slf4j
@org.springframework.stereotype.Service
public class OpenApiForwardDeviceDataServiceImpl implements OpenApiForwardDeviceDataService {
    private final IForwardEnvironmentalMonitorDataService forwardEnvironmentalMonitorDataService;
    private final IForwardEnvironmentalMonitorDataHisService forwardEnvironmentalMonitorDataHisService;
    private final IForwardInverterDataService forwardInverterDataService;
    private final IForwardInverterDataHisService forwardInverterDataHisService;
    private final IForwardCombinerBoxDataService forwardCombinerBoxDataService;
    private final IForwardCombinerBoxDataHisService forwardCombinerBoxDataHisService;
    private final IForwardStringDataCurrentdayService forwardStringDataCurrentdayService;
    private final IForwardStringDataService forwardStringDataService;
    private final IForwardStringDataHisService forwardStringDataHisService;

    public OpenApiForwardDeviceDataServiceImpl(IForwardEnvironmentalMonitorDataService forwardEnvironmentalMonitorDataService,
                                               IForwardEnvironmentalMonitorDataHisService forwardEnvironmentalMonitorDataHisService,
                                               IForwardInverterDataService forwardInverterDataService,
                                               IForwardInverterDataHisService forwardInverterDataHisService,
                                               IForwardCombinerBoxDataService forwardCombinerBoxDataService,
                                               IForwardCombinerBoxDataHisService forwardCombinerBoxDataHisService,
                                               IForwardStringDataCurrentdayService forwardStringDataCurrentdayService,
                                               IForwardStringDataService forwardStringDataService,
                                               IForwardStringDataHisService forwardStringDataHisService) {
        this.forwardEnvironmentalMonitorDataService = forwardEnvironmentalMonitorDataService;
        this.forwardEnvironmentalMonitorDataHisService = forwardEnvironmentalMonitorDataHisService;
        this.forwardInverterDataService = forwardInverterDataService;
        this.forwardInverterDataHisService = forwardInverterDataHisService;
        this.forwardCombinerBoxDataService = forwardCombinerBoxDataService;
        this.forwardCombinerBoxDataHisService = forwardCombinerBoxDataHisService;
        this.forwardStringDataCurrentdayService = forwardStringDataCurrentdayService;
        this.forwardStringDataService = forwardStringDataService;
        this.forwardStringDataHisService = forwardStringDataHisService;
    }

    @Override
    public void postEnvironment(OpenApiForwardDeviceEnvironmentDataReqVO reqVo) {
        // TODO 优化
        List<ForwardEnvironmentalMonitorDataDO> dataDos = ObjectMapper.mapList(reqVo.getDatas(), ForwardEnvironmentalMonitorDataDO.class);
//        forwardEnvironmentalMonitorDataService.saveOrUpdateDos(dataDos);
        forwardEnvironmentalMonitorDataService.insertOrUpdateBatch(dataDos);
        List<ForwardEnvironmentalMonitorDataHisDO> hisDataDos = ObjectMapper.mapList(reqVo.getDatas(), ForwardEnvironmentalMonitorDataHisDO.class);
        forwardEnvironmentalMonitorDataHisService.saveBatch(hisDataDos);
    }

    @Override
    public void postInverter(OpenApiForwardDeviceInverterDataReqVO reqVo) {
        // TODO 优化
        List<ForwardInverterDataDO> inverterDataDos = null;
        List<ForwardInverterDataHisDO> inverterDataHisDos = null;
        // 如果是组串式逆变器
        List<ForwardStringDataDO> stringDataDos = null;
        List<ForwardStringDataHisDO> stringDataHisDos = null;
        List<ForwardStringDataCurrentdayDO> stringDataCurrentdayDos = null;
        if (null != reqVo && CollectionUtils.isNotEmpty(reqVo.getDatas())) {
            inverterDataDos = new ArrayList<>(reqVo.getDatas().size());
            inverterDataHisDos = new ArrayList<>(reqVo.getDatas().size());
            stringDataDos = new ArrayList<>(reqVo.getDatas().size() * 32);
            stringDataHisDos = new ArrayList<>(reqVo.getDatas().size() * 32);
            stringDataCurrentdayDos = new ArrayList<>(reqVo.getDatas().size() * 32);
            for (OpenApiForwardDeviceInverterDataReqVO.ForwardDeviceInverterDataReqVO data : reqVo.getDatas()) {
                ForwardInverterDataDO inverterDataDo = ObjectMapper.map(data, ForwardInverterDataDO.class);
                inverterDataDos.add(inverterDataDo);
                ForwardInverterDataHisDO inverterDataHisDo = ObjectMapper.map(inverterDataDo, ForwardInverterDataHisDO.class);
                inverterDataHisDos.add(inverterDataHisDo);
                if (CollectionUtils.isNotEmpty(data.getStringDatas())) {
                    for (OpenApiForwardDeviceStringDataReqVO stringData : data.getStringDatas()) {
                        ForwardStringDataDO stringDataDo = ObjectMapper.map(stringData, ForwardStringDataDO.class);
                        stringDataDo.setDevicePid(inverterDataDo.getDeviceId());
                        stringDataDos.add(stringDataDo);
                        ForwardStringDataHisDO stringDataHisDo = ObjectMapper.map(stringDataDo, ForwardStringDataHisDO.class);
                        stringDataHisDos.add(stringDataHisDo);
                        ForwardStringDataCurrentdayDO stringDataCurrentdayDo = ObjectMapper.map(stringDataDo, ForwardStringDataCurrentdayDO.class);
                        stringDataCurrentdayDos.add(stringDataCurrentdayDo);
                    }
                }
            }
        }
        if (CollectionUtils.isEmpty(inverterDataDos) && CollectionUtils.isEmpty(inverterDataHisDos) &&
                CollectionUtils.isEmpty(stringDataDos) && CollectionUtils.isEmpty(stringDataCurrentdayDos) && CollectionUtils.isEmpty(stringDataHisDos)) {
            log.warn("逆变器数据处理-无数据保存");
            return;
        }
        if (CollectionUtils.isNotEmpty(inverterDataDos)) {
            log.info("逆变器数据处理-保存逆变器实时数据开始...");
            forwardInverterDataService.insertOrUpdateBatch(inverterDataDos);
            log.info("逆变器数据处理-保存逆变器实时数据结束{}...", inverterDataDos.size());
        }
        if (CollectionUtils.isNotEmpty(inverterDataHisDos)) {
            log.info("逆变器数据处理-保存逆变器历史数据开始...");
            forwardInverterDataHisService.saveBatch(inverterDataHisDos);
            log.info("逆变器数据处理-保存逆变器历史数据结束{}...", inverterDataHisDos.size());
        }
        if (CollectionUtils.isNotEmpty(stringDataDos)) {
            log.info("逆变器数据处理-保存组串实时数据开始...");
            forwardStringDataService.insertOrUpdateBatch(stringDataDos);
            log.info("逆变器数据处理-保存组串实时数据结束{}...", stringDataDos.size());
        }
        if (CollectionUtils.isNotEmpty(stringDataCurrentdayDos)) {
            log.info("逆变器数据处理-保存组串当天数据开始...");
            forwardStringDataCurrentdayService.insertIgnoreBatch(stringDataCurrentdayDos);
            log.info("逆变器数据处理-保存组串当天数据结束{}...", stringDataCurrentdayDos.size());
        }
        if (CollectionUtils.isNotEmpty(stringDataHisDos)) {
            log.info("逆变器数据处理-保存组串历史数据开始...");
            forwardStringDataHisService.saveBatch(stringDataHisDos);
            log.info("逆变器数据处理-保存组串历史数据结束{}...", stringDataHisDos.size());
        }
    }

    @Override
    public void postCombinerbox(OpenApiForwardDeviceCombinerboxDataReqVO reqVo) {
        // TODO 优化
        List<ForwardCombinerBoxDataDO> combinerBoxDataDos = null;
        List<ForwardCombinerBoxDataHisDO> combinerBoxDataHisDos = null;
        // 如果是汇流箱式组串
        List<ForwardStringDataDO> stringDataDos = null;
        List<ForwardStringDataHisDO> stringDataHisDos = null;
        List<ForwardStringDataCurrentdayDO> stringDataCurrentdayDos = null;
        if (null != reqVo && CollectionUtils.isNotEmpty(reqVo.getDatas())) {
            if (null != reqVo && CollectionUtils.isNotEmpty(reqVo.getDatas())) {
                combinerBoxDataDos = new ArrayList<>(reqVo.getDatas().size());
                combinerBoxDataHisDos = new ArrayList<>(reqVo.getDatas().size());
                stringDataDos = new ArrayList<>(reqVo.getDatas().size() * 32);
                stringDataHisDos = new ArrayList<>(reqVo.getDatas().size() * 32);
                stringDataCurrentdayDos = new ArrayList<>(reqVo.getDatas().size() * 32);
                for (OpenApiForwardDeviceCombinerboxDataReqVO.ForwardDeviceCombinerboxDataReqVO data : reqVo.getDatas()) {
                    ForwardCombinerBoxDataDO combinerBoxDataDo = ObjectMapper.map(data, ForwardCombinerBoxDataDO.class);
                    combinerBoxDataDos.add(combinerBoxDataDo);
                    ForwardCombinerBoxDataHisDO combinerBoxDataHisDo = ObjectMapper.map(combinerBoxDataDo, ForwardCombinerBoxDataHisDO.class);
                    combinerBoxDataHisDos.add(combinerBoxDataHisDo);
                    if (CollectionUtils.isNotEmpty(data.getStringDatas())) {
                        for (OpenApiForwardDeviceStringDataReqVO stringData : data.getStringDatas()) {
                            ForwardStringDataDO stringDataDo = ObjectMapper.map(stringData, ForwardStringDataDO.class);
                            stringDataDo.setDevicePid(combinerBoxDataDo.getDeviceId());
                            stringDataDos.add(stringDataDo);
                            ForwardStringDataHisDO stringDataHisDo = ObjectMapper.map(stringDataDo, ForwardStringDataHisDO.class);
                            stringDataHisDos.add(stringDataHisDo);
                            ForwardStringDataCurrentdayDO stringDataCurrentdayDo = ObjectMapper.map(stringDataDo, ForwardStringDataCurrentdayDO.class);
                            stringDataCurrentdayDos.add(stringDataCurrentdayDo);
                        }
                    }
                }
            }
            if (CollectionUtils.isEmpty(combinerBoxDataDos) && CollectionUtils.isEmpty(combinerBoxDataHisDos) &&
                    CollectionUtils.isEmpty(stringDataDos) && CollectionUtils.isEmpty(stringDataCurrentdayDos) && CollectionUtils.isEmpty(stringDataHisDos)) {
                log.warn("汇流箱数据处理-无数据保存");
                return;
            }
            if (CollectionUtils.isNotEmpty(combinerBoxDataDos)) {
                log.info("汇流箱数据处理-保存汇流箱实时数据开始...");
                forwardCombinerBoxDataService.insertOrUpdateBatch(combinerBoxDataDos);
                log.info("汇流箱数据处理-保存汇流箱实时数据结束{}...", combinerBoxDataDos.size());
            }
            if (CollectionUtils.isNotEmpty(combinerBoxDataHisDos)) {
                log.info("汇流箱数据处理-保存汇流箱历史数据开始...");
                forwardCombinerBoxDataHisService.saveBatch(combinerBoxDataHisDos);
                log.info("汇流箱数据处理-保存汇流箱历史数据结束{}...", combinerBoxDataHisDos.size());
            }
            if (CollectionUtils.isNotEmpty(stringDataDos)) {
                log.info("汇流箱数据处理-保存组串实时数据开始...");
                forwardStringDataService.insertOrUpdateBatch(stringDataDos);
                log.info("汇流箱数据处理-保存组串实时数据结束{}...", stringDataDos.size());
            }
            if (CollectionUtils.isNotEmpty(stringDataCurrentdayDos)) {
                log.info("汇流箱数据处理-保存组串当天数据开始...");
                forwardStringDataCurrentdayService.insertIgnoreBatch(stringDataCurrentdayDos);
                log.info("汇流箱数据处理-保存组串当天数据结束{}...", stringDataCurrentdayDos.size());
            }
            if (CollectionUtils.isNotEmpty(stringDataHisDos)) {
                log.info("汇流箱数据处理-保存组串历史数据开始...");
                forwardStringDataHisService.saveBatch(stringDataHisDos);
                log.info("汇流箱数据处理-保存组串历史数据结束{}...", stringDataHisDos.size());
            }
        }
    }
}
